//this file define GDomainObject interface
//for post changed event and data to outside
//andycai.sc@gmail 2012-04-13

#ifndef GDOMAINOBJECT_H
#define	GDOMAINOBJECT_H

//define GDomainEvent for transport event that the domain
//and data for transport
class GDomainEvent
{
public:
	GDomainEvent(long eventType, void* data) :
	  m_eventType(eventType), m_data(data)
    {
    }
	virtual ~GDomainEvent() {}

    void setEvent(long eventType) {	m_eventType = eventType; }
    void setData(void* data) { m_data = data; }

    long getEvent() { return m_eventType; }
    void* getData() { return m_data; }

protected:
	long m_eventType;
	void* m_data;
};

class GDomainObject;
//define GDataReceiver interface for transport to different components
class GDataReceiver
{
public:
	virtual ~GDataReceiver() {}

	virtual void set(GDomainObject* domainObj) {}

	virtual void clear(GDomainObject* domainObj) {}

	virtual void process(GDomainEvent* dEvent, GDomainObject* dObject) = 0;

protected:
	GDataReceiver() {}
};

//define GDomainObject for DataReceiver transport
class GDomainObject
{
public:
	virtual ~GDomainObject() {}

	void setDataReceiver(GDataReceiver* dataReceiver)
	{
	    m_dataReceiver = dataReceiver;
    }

protected:
	void post(long eventType, void* data, GDataReceiver* receiver = 0)
	{
		GDomainEvent dEvent(eventType, data);
		post(dEvent, receiver);
	}

	void post(GDomainEvent& event, GDataReceiver* receiver = 0)
	{
	    if (receiver != 0)
			receiver->process(&event, this);
		else if (m_dataReceiver != 0)
			m_dataReceiver->process(&event, this);
	}

protected:
	GDomainObject(GDataReceiver* dataReceiver = 0): m_dataReceiver(dataReceiver)
	{
	}
	GDataReceiver* m_dataReceiver;
};

#endif
